/**
  ******************************************************************************
  * @file    xxx.c 
  * @author  SD Electronics xxx
  * @version V1.0
  * @date    xx-xx-xx
  * @brief   xxxxxxxxxxxxxxxxxx.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "sd_common.h"


/** 
  * @brief  Beep 硬件配置相关结构体 
  */ 
ST_Beep_PHY g_stBeepPhyParam = 
{
    GPIO_PORT_C, GPIO_BIT_7
};


/** 
  * @brief Beep 控制结构体 
  */ 
ST_Beep_CTRL g_stBeepCtrl = 
{                       
    false,
    &g_stBeepPhyParam,
};  

/**
  * @}
  */


/**
  * @brief  SDInitBeep
  * @note   xxx
  * @param  None
  * @retval None
  */
void SDInitBeep(PST_Beep_PHY pstBeepCntPhy)
{
    #if 1
    pmu_set_port_mux(GPIO_PORT_C, GPIO_BIT_7, PMU_PORT_MUX_GPIO);
	pmu_set_pin_to_PMU(GPIO_PORT_C, BIT(7));
	pmu_set_pin_dir(GPIO_PORT_C, BIT(7), GPIO_DIR_OUT);
	pmu_set_pin_pull(GPIO_PORT_C, BIT(7), false);
    //pwm_init(PWM_CHANNEL_5, 1000, 50);
    pmu_set_gpio_value(GPIO_PORT_C, BIT(7), 0);
    #else
    pmu_pwm_init(); 
    pmu_set_pin_to_PMU(GPIO_PORT_C, BIT(7)); 
    pmu_set_pin_dir(GPIO_PORT_C, BIT(7), true); 
    pmu_set_port_mux(GPIO_PORT_C, GPIO_BIT_7, PMU_PORT_MUX_PWM);
    pmu_pwm_set_param(GPIO_PORT_C, GPIO_BIT_7, 100, 50); 
    #endif
    printf("usr: sd Beep init.\r\n");
}

/**
  * @brief  BeepOpen
  * @note   xxx
  * @param  None
  * @retval None
  */
static long BeepOpen(void *pvHandle, unsigned long ulOption)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;

    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_Beep_CTRL pstBeepCtrl = (PST_Beep_CTRL)pstDevManage->pvData;
        if(false == pstBeepCtrl->bInitFlag)
        {
			/* Beep初始化 */
            SDInitBeep(pstBeepCtrl->pstBeepInfo);
            pstBeepCtrl->bInitFlag = true;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;
}  

/**
  * @brief  BeepRead
  * @note   xxx
  * @param  None
  * @retval None
  */
static long BeepRead(void *pvHandle, unsigned long ulOpertion, unsigned char *pucDataBuf, int* ulDataSize)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_Beep_CTRL pstBeepCtrl = (PST_Beep_CTRL)pstDevManage->pvData;
        //
        if(true == pstBeepCtrl->bInitFlag)
        {
            ;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;    
}

/**
  * @brief  BeepWrite
  * @note   xxx
  * @param  None
  * @retval None
  */
static long BeepWrite(void *pvHandle, unsigned long ulOpertion, unsigned char *pucDataBuf, unsigned long ulDataLen)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_Beep_CTRL pstBeepCtrl = (PST_Beep_CTRL)pstDevManage->pvData;
        //
        if(true == pstBeepCtrl->bInitFlag)
        {
           ;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;
}

/**
  * @brief  BeepControl
  * @note   xxx
  * @param  None
  * @retval None
  */
static long BeepControl (void *pvHandle,unsigned long ulOpertion, unsigned long ulControlCmd, void *pvd)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    { 
        PST_Beep_CTRL pstBeepCtrl = (PST_Beep_CTRL)pstDevManage->pvData;
        if(true == pstBeepCtrl->bInitFlag)
        {
            switch(ulOpertion)
            {
                case EN_BEEP_ON: 
                //set H level
                //printf("[EN_BEEP_ON]\r\n");
                //pwm_start(PWM_CHANNEL_5);
                pmu_set_gpio_value(GPIO_PORT_C, BIT(7), 1);
                // pmu_pwm_start(GPIO_PORT_C, GPIO_BIT_7, true, true); 
                break;
                case EN_BEEP_OFF:
                //set L level
                //printf("[EN_BEEP_OFF]\r\n");
                pmu_set_gpio_value(GPIO_PORT_C, BIT(7), 0);
                //pmu_pwm_start(GPIO_PORT_C, GPIO_BIT_7, true, true); 
                break;
                default:
                break;
            }
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;             
}

/**
  * @brief  BeepClose
  * @note   xxx
  * @param  None
  * @retval None
  */
static long BeepClose (void *pvHandle)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;     
}



/**
  * @brief  g_stBeepCtlFUN
  * @note  xxx
  * @param  None
  * @retval None
  */
ST_CTR_FUN g_stBeepCtlFUN = 
{
    BeepOpen,		//打开
    BeepRead,		//读
    BeepWrite,		//写
    BeepControl,	//控制
    BeepClose    	//关闭
};


/******************* (C) COPYRIGHT 2011 ShenZhenShiding electronics **************************/
